package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.UserMapper;
import top.maof.book.model.User;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.UserService;
import top.maof.book.utils.Utils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public PageData<User> findPage(PageWrap<User> pageWrap) {
        IPage<User> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(User::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getUsername() != null) {
            queryWrapper.lambda().eq(User::getUsername, pageWrap.getModel().getUsername());
        }
        if (pageWrap.getModel().getPassword() != null) {
            queryWrapper.lambda().eq(User::getPassword, pageWrap.getModel().getPassword());
        }
        if (pageWrap.getModel().getNickname() != null) {
            queryWrapper.lambda().eq(User::getNickname, pageWrap.getModel().getNickname());
        }
        if (pageWrap.getModel().getSalt() != null) {
            queryWrapper.lambda().eq(User::getSalt, pageWrap.getModel().getSalt());
        }
        if (pageWrap.getModel().getEmail() != null) {
            queryWrapper.lambda().eq(User::getEmail, pageWrap.getModel().getEmail());
        }
        if (pageWrap.getModel().getPhoneNumber() != null) {
            queryWrapper.lambda().eq(User::getPhoneNumber, pageWrap.getModel().getPhoneNumber());
        }
        if (pageWrap.getModel().getImg() != null) {
            queryWrapper.lambda().eq(User::getImg, pageWrap.getModel().getImg());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(userMapper.selectPage(page, queryWrapper));
    }


    @Override
    public int add(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User find(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int delete(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User getWithRole(int id) {
        return userMapper.getWithRole(id);
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public User selectByUEP(String username, String email, String phoneNumber) {
        return userMapper.selectByUEP(username, email, phoneNumber);
    }
}
